`timescale  1ns/1ns
module tb_horizon_projection();

localparam image_width  = 1024;
localparam image_height = 600;
//----------------------------------------------------------------------
//  clk & rst_n
reg                             clk;
reg                             rst_n;

initial
begin
    clk = 1'b0;
    forever #5 clk = ~clk;
end

initial
begin
    rst_n = 1'b0;
    repeat(50) @(posedge clk);
    rst_n = 1'b1;
end

//----------------------------------------------------------------------
//  Image data prepred to be processed
reg                             per_img_vsync;
reg                             per_img_href ;
reg                             per_img_valid;
reg             [7:0]           per_img_data;

//  Image data has been processed
//----------------------------------------------------------------------
//  task and function
task image_input;
    bit             [31:0]      row_cnt;
    bit             [31:0]      col_cnt;
    bit             [7:0]       mem     [image_width*image_height-1:0];
    $readmemh("D:/Matlab/Matlab_Projects/Hai_Yun_Cup/step_1/img_Gray2.dat",mem);
    
    for(row_cnt = 0;row_cnt < image_height;row_cnt++)
    begin
        repeat(5) @(posedge clk);
        per_img_vsync = 1'b0;
        repeat(5) @(posedge clk);
        for(col_cnt = 0;col_cnt < image_width;col_cnt++)
        begin
            per_img_valid = ~per_img_valid;
            per_img_href  = 1'b1;
            per_img_data  = mem[row_cnt*image_width+col_cnt];;
            @(posedge clk);
        end
        per_img_href  = 1'b0;
    end

    per_img_vsync = 1'b1;
    @(posedge clk);
    
endtask : image_input

task image_input_2;
    bit             [31:0]      row_cnt;
    bit             [31:0]      col_cnt;
    bit             [7:0]       mem     [image_width*image_height-1:0];
    $readmemh("D:/Matlab/Matlab_Projects/Hai_Yun_Cup/step_1/img_Gray3.dat",mem);
    
    for(row_cnt = 0;row_cnt < image_height;row_cnt++)
    begin
        repeat(5) @(posedge clk);
        per_img_vsync = 1'b0;
        repeat(5) @(posedge clk);
        for(col_cnt = 0;col_cnt < image_width;col_cnt++)
        begin
            per_img_valid = ~per_img_valid;
            per_img_href  = 1'b1;
            per_img_data  = mem[row_cnt*image_width+col_cnt];;
            @(posedge clk);
        end
        per_img_href  = 1'b0;
    end
    
    per_img_vsync = 1'b1;
    @(posedge clk);
    
endtask : image_input_2

horizon_projection u_horizon_projection(
    .clk                     ( clk                     ),
    .rst_n                   ( rst_n                   ),

    .per_frame_vsync         ( per_img_vsync         ),
    .per_frame_href          ( per_img_href          ),
    .per_frame_clken         ( 1'd1         ),
    .per_img_Bit             ( per_img_data[0]       ),

    .prj_x_start             ( 10'd10       ),
    .prj_x_end               ( image_width-10 ),
    .prj_y_start             (10'd5         ),            
    .prj_y_end               (image_height-5   )
);

initial
begin
    per_img_vsync = 0;
    per_img_href  = 0;
    per_img_valid = 0;
end

initial 
begin
    wait(rst_n);
    repeat(5) @(posedge clk); 

    image_input;
    repeat(1000) @(posedge clk); 

    image_input_2;
    repeat(1000) @(posedge clk); 
    $stop;
end 

endmodule